package com.itcast.main.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itcast.main.dao.system.MenuRepository;
import com.itcast.main.domain.system.Menu;
import com.itcast.main.domain.system.User;
import com.itcast.main.service.system.MenuService;
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuRepository menuRepository;
	
	
	@Override
	public List<Menu> findAll() {
		return menuRepository.findAll();
	}


	@Override
	public void save(Menu model) {
		if (model.getParentMenu() != null && model.getParentMenu().getId()==0) {
			model.setParentMenu(null);
		}
		//调用DAO
		menuRepository.save(model);
	}


	@Override
	public List<Menu> findByUser(User user) {
		// 针对admin用户显示 所有菜单
		if (user.getUsername().equals("admin")) {
			return menuRepository.findAll();
		}else {
			//根据用户 角色查询菜单
			return menuRepository.findByUser(user.getId());
		}
	}
}
